﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapBSGROE(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveBSGROE(DataRow dwData, DataRow dwDispo)
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryBSGROE(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapBSGROE(ref dwLocal, dwData);

            if (!flgExist)
            {
                InsertBSGROE(dwLocal);
            }
            UpdateBSGROE(dwLocal);
        }
        public void InsertBSGROE(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "INSERT INTO BSGROE(BSGMANDANT, BSGART, BSGMTYP, BSGLFD, BSGAENDZL, BSGAENDBEN, BSGAENDDAT, BSGNEUBEN, BSGNEUDAT, BSGKETTART, BSGMAXLG, BSGMAXGW, BSGLOS, BSGANZTB, BSGLLEIST, BSGRLEIST, BSGFILLER)"
+ "VALUES (:BSGMANDANT, :BSGART,:BSGMTYP, 1, 1, '        ', 0, :BSGNEUBEN, :BSGNEUDAT, :BSGKETTART, 0, 0, 0, 0, '    ', '    ', '                                    ')";
            or1.Parameters.Add("BSGMANDANT", OracleType.NVarChar).Value = dwData["BSGMANDANT"].ToString();
            or1.Parameters.Add("BSGART", OracleType.NVarChar).Value = dwData["BSGART"].ToString();
            or1.Parameters.Add("BSGMTYP", OracleType.NVarChar).Value = dwData["BSGMTYP"].ToString();
            or1.Parameters.Add("BSGKETTART", OracleType.NVarChar).Value = dwData["BSGKETTART"].ToString();
            or1.Parameters.Add("BSGNEUBEN", OracleType.NVarChar).Value = dwData["BSGNEUBEN"].ToString();
            or1.Parameters.Add("BSGNEUDAT", OracleType.Number).Value = dwData["BSGNEUDAT"].ToString();
            setResult = Connect2Proxy(or1);

        }
        public void DeleteBSGROE(string client, string art)
        {

            OracleCommand or1;

            or1 = OracleDB.CreateCommand();
            or1.CommandText = "DELETE  BSGROE"
           + " WHERE "
            + " BSGMANDANT=:BSGMANDANT AND BSGART=:BSGART";
            or1.Parameters.Add("BSGMANDANT", OracleType.NVarChar).Value = client;
            or1.Parameters.Add("BSGART", OracleType.NVarChar).Value = art;
            Connect2Proxy(or1);
        }
        public void UpdateBSGROE(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE BSGROE SET "
                + "BSGMANDANT=:BSGMANDANT,"
                + "BSGART=:BSGART,"
                + "BSGMTYP=:BSGMTYP,"
                + "BSGLFD=:BSGLFD,"
                + "BSGAENDZL=:BSGAENDZL,"
                + "BSGAENDBEN=:BSGAENDBEN,"
                + "BSGAENDDAT=:BSGAENDDAT,"
                + "BSGNEUBEN=:BSGNEUBEN,"
                + "BSGNEUDAT=:BSGNEUDAT,"
                + "BSGKETTART=:BSGKETTART,"
                + "BSGMAXLG=:BSGMAXLG,"
                + "BSGMAXGW=:BSGMAXGW,"
                + "BSGLOS=:BSGLOS,"
                + "BSGANZTB=:BSGANZTB,"
                + "BSGLLEIST=:BSGLLEIST,"
                + "BSGRLEIST=:BSGRLEIST,"
                + "BSGFILLER=:BSGFILLER "
                + " WHERE  "
                + "  BSGMANDANT=:BSGMANDANT AND BSGART=:BSGART AND BSGMTYP=:BSGMTYP ";
            or1.Parameters.Add("BSGMANDANT", OracleType.NVarChar).Value = dwData["BSGMANDANT"].ToString();
            or1.Parameters.Add("BSGART", OracleType.NVarChar).Value = dwData["BSGART"].ToString();
            or1.Parameters.Add("BSGMTYP", OracleType.NVarChar).Value = dwData["BSGMTYP"].ToString();
            or1.Parameters.Add("BSGLFD", OracleType.NVarChar).Value = dwData["BSGLFD"].ToString();
            or1.Parameters.Add("BSGAENDZL", OracleType.NVarChar).Value = dwData["BSGAENDZL"].ToString();
            or1.Parameters.Add("BSGAENDBEN", OracleType.NVarChar).Value = dwData["BSGAENDBEN"].ToString();
            or1.Parameters.Add("BSGAENDDAT", OracleType.NVarChar).Value = dwData["BSGAENDDAT"].ToString();
            or1.Parameters.Add("BSGNEUBEN", OracleType.NVarChar).Value = dwData["BSGNEUBEN"].ToString();
            or1.Parameters.Add("BSGNEUDAT", OracleType.NVarChar).Value = dwData["BSGNEUDAT"].ToString();
            or1.Parameters.Add("BSGKETTART", OracleType.NVarChar).Value = dwData["BSGKETTART"].ToString();
            or1.Parameters.Add("BSGMAXLG", OracleType.NVarChar).Value = dwData["BSGMAXLG"].ToString();
            or1.Parameters.Add("BSGMAXGW", OracleType.NVarChar).Value = dwData["BSGMAXGW"].ToString();
            or1.Parameters.Add("BSGLOS", OracleType.NVarChar).Value = dwData["BSGLOS"].ToString();
            or1.Parameters.Add("BSGANZTB", OracleType.NVarChar).Value = dwData["BSGANZTB"].ToString();
            or1.Parameters.Add("BSGLLEIST", OracleType.NVarChar).Value = dwData["BSGLLEIST"].ToString();
            or1.Parameters.Add("BSGRLEIST", OracleType.NVarChar).Value = dwData["BSGRLEIST"].ToString();
            or1.Parameters.Add("BSGFILLER", OracleType.NVarChar).Value = dwData["BSGFILLER"].ToString();
            Connect2Proxy(or1);
        }
        public DataSet QueryBSGROE(DataRow dwData, int option = 1)
        {
            DataSet setReturn = new DataSet();
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM BSGROE WHERE  ROWNUM<2 ";
                    break;
                case 1:
                    or1.CommandText = "SELECT * FROM  BSGROE"
                        + " WHERE "
                        + " BSGMANDANT=:BSGMANDANT AND BSGART=:BSGART AND BSGMTYP=:BSGMTYP";
                    or1.Parameters.Add("BSGMANDANT", OracleType.NVarChar).Value = dwData["BSGMANDANT"].ToString();
                    or1.Parameters.Add("BSGART", OracleType.NVarChar).Value = dwData["BSGART"].ToString();
                    or1.Parameters.Add("BSGMTYP", OracleType.NVarChar).Value = dwData["BSGMTYP"].ToString();
                    break;
                case 2:
                    or1 = OracleDB.CreateCommand();
                    or1.CommandText = "SELECT DISTINCT  WF.* FROM WDECK WD "
                        + " JOIN WASTKE ON WDMANDANT=WAKMANDANT AND WDART=WAKART AND WDVAR=WAKVAR AND WDFARBE=WAKWCOL"
                        + " JOIN BSGROE WF ON BSGMANDANT=WAKMANDANT AND BSGART=WAKKART "
                        + " WHERE WDMANDANT=:WDMANDANT AND WDSA='K' AND WDTYP='B' AND WDWAKZ='F' AND WDAUFKRS=:WDAUFKRS  AND WDAUFLFD=:WDAUFLFD";
                    or1.Parameters.Add("WDMANDANT", OracleType.NVarChar).Value = dwData["WDMANDANT"].ToString();
                    or1.Parameters.Add("WDAUFKRS", OracleType.NVarChar).Value = dwData["WDAUFKRS"].ToString();
                    or1.Parameters.Add("WDAUFLFD", OracleType.NVarChar).Value = dwData["WDAUFLFD"].ToString();
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                tbLocal.TableName = "BSGROE";

            }
            setReturn.Merge(Result);
            return setReturn;
        }
    }
}
